<?php
namespace app\smallapp\controller;
use app\common\controller\BaseApi;
/**
 * 小程序 会员端
 * */
class Member extends BaseApi{
	function _initialize() {
		if(!request()->isPost()){
			return $this->api_ajax_error("","非法访问");
		}
		$time=input("time/d","");
		$sign=input("sign/s","");
		if(input("debug")!=1){ 
			if(($time+300)<time()||($time-300)>time()){//接口调用时间操过5分钟
				return $this->api_ajax_error("","链接已经过期");
			}
			if($sign!=md5("ocmcom_me_".$time)){
				return $this->api_ajax_error("","签名错误！");
			}	
		}
    }
	
	/**
	 * 根据电话号码获取店铺
	 * */
	function GetShopList(){
		$mobile=input("mobile");
		if(strlen($mobile)!=11){
			return $this->api_ajax_error("","手机号码格式错误");
		}
		$list=db("GymMember")->alias("a")->field("b.id,b.shop_name")->join([["m_shop b","a.shop_id=b.id","left"]])->where("a.mobile='$mobile'")->select();
		if(empty($list)){
			return $this->api_ajax_error([],"无效的手机账号");
		}else{
			return $this->api_ajax_success($list);
		}
	}
	
	/**
	 * 登录
	 * http://smartgym.com/smallapp/Member/Login
	 * */
	function Login(){
		$moblie=input("mobile/s");
		$psw=input("psw/s");
		$shop_id=input("shop_id");
		if(empty($shop_id)){
			return $this->api_ajax_error("","店铺id 为空");
		}
		if(strlen($moblie)!=11){
			return $this->api_ajax_error("","手机号码格式错误！");
		}
		if(strlen($psw)<5){
			return $this->api_ajax_error("","密码长度不足5位！");
		}
		$model=db("GymMember")->where(" mobile='$moblie' and psw='$psw' and shop_id=$shop_id")->find();
		if(empty($model)){
			return $this->api_ajax_error("","账号或密码错误！");
		}else{
			if($model['web_open_up']!=1){
				return $this->api_ajax_error("","您还不是正式会员");
			}
			$data=array(
						"member_id"=>$model['id'],
						"member_name"=>$model["name"],
						"shop_id"=>$model["shop_id"],
						"shop_name"=>db("Shop")->where("id=".$model['shop_id'])->value("shop_name"),
						"face1"=>$model['face1'],
						"face1_status"=>$model['face1_status'],
						"member_auth"=>db("SmallappAuth")->where("shop_id=$shop_id")->value("auth_code"),
			);
			return $this->api_ajax_success($data);
		}
	}
	/*
	 * 获取短信验证码
	 * http://smartgym.com/smallapp/Member/GetCode
	 * */
	function GetCode(){
		$mobile=input("mobile/s");
		$model=db("GymMember")->where("mobile='$mobile'")->find();
		if(empty($model)){
			return $this->api_ajax_error("","电话号码不存在");
		}else{
			if(empty(cache("$mobile"))){
				$result=send_msg_code($mobile);
				if($result['flag']){
					cache("$mobile",$result['data'],60);
					return $this->api_ajax_success("","成功");
				}else{
					return $this->api_ajax_error($result['data'],$result['msg']);
				}
			}else{
				return $this->api_ajax_error("","操作频繁，稍后再试！");
			}
		}
	} 
	/**
	 * 修改密码 
	 *  http://smartgym.com/smallapp/Member/ModifyPsw
	 */
	function ModifyPsw(){
		$mobile=input("mobile/s");
		$new_psw=input("new_psw/s");
		$old_psw=input("old_psw/s");
		$shop_id=input("shop_id");
		if(strlen(trim($new_psw))<5){
			return $this->api_ajax_error("","新密码长度必须大于5位");
		}
		$id=db("GymMember")->where("mobile='$mobile' and psw='$old_psw' and shop_id=$shop_id ")->value("id");
		if(empty($id)){
			return $this->api_ajax_error("","会员账号或密码错误");
		}else{
			db("GymMember")->where("id=$id")->setField(array("psw"=>$new_psw));
			return $this->api_ajax_success();
		}
	}
	/**
	 * 找回密码 
	 *  http://smartgym.com/smallapp/Member/FindPsw
	 */
	function FindPsw(){
		$mobile=input("mobile/s");
		$new_psw=input("psw/s");
		$shop_id=input("shop_id/d");
		$code=input("code","xxxx");
		if(strlen(trim($new_psw))<5){
			return $this->api_ajax_error("","新密码长度必须大于5位");
		}
		if(empty($shop_id)){
			return $this->api_ajax_error("","店铺id为空");
		}
		if($code!=cache("$mobile")){
			return $this->api_ajax_error("","验证码错误或者过期");
		}
		
		$id=db("GymMember")->where("mobile='$mobile' and shop_id=$shop_id")->value("id");
		if(empty($id)){
			return $this->api_ajax_error("","pt账号不存在");
		}else{
			db("GymMember")->where("id=$id")->setField(array("psw"=>$new_psw));
			return $this->api_ajax_success("");
		}
	}
	/**
	 * 会员卡列表
	 *  http://smartgym.com/smallapp/Member/CardList
	 * */
	function CardList(){
		$shop_id=input("shop_id");
		$member_id=input("member_id");
		if(empty($shop_id)||empty($member_id)){
			return $this->api_ajax_error("","参数错误");
		}
		$list=db("GymOrder")->field("card_no,start_date,end_date,able_count,able_money,gym_card_name,gym_card_type,id as order_id,pay_money")->where("shop_id=$shop_id and gym_member_id=$member_id and is_delete=0")->select();
		if(empty($list)){
			return $this->api_ajax_error("","抱歉！没有更多的数据");
		}else{
			return $this->api_ajax_success($list);
		}
	}
		/**
		 * 会员卡详情
		 * http://smartgym.com/smallapp/Member/CardDedail
		 * */
	function CardDedail(){
		$order_id=input("order_id");
		$member_id=input("member_id");
		$shop_id=input("shop_id");
		if(empty($order_id)||empty($member_id)||empty($shop_id)){
			return $this->api_ajax_error("","参数错误");
		}
		$order=db("GymOrder")->field("card_no,start_date,end_date,able_count,able_money,gym_card_name,gym_card_type,id as order_id,member_ship_id as pt_id,buy_count,able_count,pay_money")->where("shop_id=$shop_id and id=$order_id and is_delete=0")->find();
		if(empty($order)){
			return $this->api_ajax_error("","无效的order_id");
		}else{
			$pt_name=db("GymEmployee")->where("shop_id=$shop_id and id=".$order["pt_id"])->value("name");
			$order['pt_name']=$pt_name;	
		}
		return $this->api_ajax_success($order);
	}
	
	/***
	 * 私教课程列表
	 * */
	function PrivateLessonList(){
		$member_id=input("member_id");
		$shop_id=input("shop_id");
		$page=input("page",1);
		if(empty($member_id)||empty($shop_id)){
			return $this->api_ajax_error("","参数错误");
		}
		$filed="course_name,course_count,course_icon,surplus,start_date,end_date,id as pt_order_id";
		$list=db("GymPtOrder")->field($filed)->where("shop_id=$shop_id and member_id=$member_id")->page($page,10)->select();
		if(empty($list)){
			return $this->api_ajax_error("","抱歉！没有更多的数据");
		}else{
			return $this->api_ajax_success($list);
		}
	}
	
	/**
	 * 私教课详情
	 * */
	function PrivateLessonDetail(){
		$pt_order_id=input("pt_order_id");
		$shop_id=input("shop_id");
		$filed="course_name,price,course_count,course_icon,surplus,start_date,end_date,pt_id,id as pt_order_id,member_id";
		$model=db("GymPtOrder")->field($filed)->where("shop_id=$shop_id and id=$pt_order_id")->find();
		if(empty($model)){
			return $this->api_ajax_error("","无效的pt_order_id");
		}else{
			$model['pt_name']=db("GymEmployee")->where("shop_id=$shop_id and id=".$model["pt_id"])->value("name");
			$model['card_no']=db("GymOrder")->where("gym_member_id=".$model['member_id']." and card_no is not null and is_delete=0")->order("id desc")->value("card_no");
		}
		return $this->api_ajax_success($model);
	}
	
	/**
	 * 所有预约
	 * @param serach_day
	 * */
	 function AllOrder(){
	 	$member_id=input("member_id");
		$search_day=input("search_day");
		$shop_id=input("shop_id");
		$page=input("page",1);
		$course_status=input("course_status");
		if(empty($member_id)||empty($shop_id)){
			return $this->api_ajax_error("","参数错误");
		}
		$where="";
		if($course_status==0){
			$where=" and a.course_status=0";
		}else if($course_status==1){
			$where=" and a.course_status=1";
		}
		
		if(!empty($search_day)){
			$where.=" and a.order_date='$search_day'";
		}
		$field="a.member_id,a.pt_order_id,a.order_date,a.order_time,a.course_count,b.course_name,b.course_icon,c.name as pt_name";
		$list=db("GymPtFollow")->alias("a")->field($field)->join([["m_gym_pt_order b","a.pt_order_id=b.id","left"],["m_gym_employee c","a.pt_id=c.id","left"]])->where("a.member_id=$member_id and a.shop_id=$shop_id and a.type=2 $where ")->page($page,10)->select();
		if(empty($list)){
			return $this->api_ajax_error("","抱歉！没有更多的数据");	
		}
		return $this->api_ajax_success($list);
	 }
	 
	 /**
	  * 我的运动
	  * */
	 function MySport(){
	 	$member_id=input("member_id");
		$shop_id=input("shop_id");
		if(empty($member_id)||empty($shop_id)){
			return $this->api_ajax_error("","参数错误");
		}else{
			$statistic=db("GymMember")->where("id=$member_id and shop_id=$shop_id")->field("sport_time,over_private_lesson")->find();
			if(empty($statistic)){
				return $this->api_ajax_error("","无效的member_id");
			}else{
				$statistic["punch_days"]=db("BehaviorLog")->fetchsql(FALSE)->where("shop_id=$shop_id and card_no in(select card_no from m_gym_order where member_id=$member_id)")->count("DISTINCT(TO_DAYS(create_time))");
				return $this->api_ajax_success($statistic);
			}
		}	  
	 }
	 
	 /**
	  * 打卡列表
	  * */
	 function PunchList(){
	 	$member_id=input("member_id");
		$shop_id=input("shop_id");
		$search_month=input("search_month",date("Y-m"));
		if(empty($member_id)||empty($shop_id)||empty($search_month)){
			return $this->api_ajax_error("","参数错误");
		}else{
			$list=db("BehaviorLog")->where("shop_id=$shop_id and member_id=$member_id and date_format(create_time,'%Y-%m')='$search_month' ")->column("distinct(date_format(create_time,'%Y-%m-%d'))");
			return $this->api_ajax_success($list);
		}
	 }
	 
	
	/**
	 * 人脸3d 模型
	 * http://smartgym.com/smallapp/Member/Member3DImage
	*/
	function Member3DImage(){
		$shop_id=input("shop_id");
		$member_id=input("member_id");
		if(empty($shop_id)||empty($member_id)){
			return $this->api_ajax_error("","参数错误");
		}
		$card_nos=db("Interface")->where("card_no in (select card_no from m_gym_order where member_id='$member_id' and shop_id=$shop_id)")->column("card_no");
		if(empty($card_nos)){
			return $this->api_ajax_error("","此会员没有购卡！");
		}else{
			$card_nos=implode(",",$card_nos);
		}
		if(empty($_FILES['file'])){
			return $this->api_ajax_error("","图片为空");
		}
		if($_FILES['file']['size']>100000){
			$this->api_ajax_error("","图片不能大于100k");
		}
		if(strpos($_FILES['file']['type'],"image")===false){
			$this->api_ajax_error("","图片格式有误".$_FILES['file']['type']);
		}
	 	$file = request()->file('file');
	 	if(!empty($file)){
	 		$dir=ROOT_PATH . 'data' . DS . 'face'.DS.$shop_id;
	 		if(!file_exists($dir)){
				mkdir($dir);
			}
	 	 	$info = $file->move($dir);
			$headimage_url=$dir."/".date("Ymd")."/".$info->getFilename();
			$this->_save_face_code($headimage_url,$card_nos,$shop_id);
			return $this->api_ajax_success("https://".$_SERVER['HTTP_HOST']."/".str_replace(ROOT_PATH, "/", $headimage_url));
	 	 }else{
	 	 	return $this->api_ajax_error("","文件为空");
	 	}
	}
	
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function _save_face_code($icon1,$card_nos,$shop_id){
		$member_id=input("member_id");
		db("FaceCode")->where("shop_id=$shop_id  and card_no in ($card_nos)")->delete();
		$face1=$this->_get_face_code($icon1);
		if(strlen($face1)<10000){
			db("GymMember")->where("id=$member_id")->setField(["face1_status"=>1,"face1"=>"https://".$_SERVER['HTTP_HOST']."/".str_replace(ROOT_PATH, "/", $icon1)]);
			return $this->api_ajax_error("https://".$_SERVER['HTTP_HOST']."/".str_replace(ROOT_PATH, "/", $icon1),"无效的图片！请重新上传");
		}
		$card_nos=explode(",", $card_nos);
		foreach ($card_nos as $key => $value) {
			db("FaceCode")->insert(array("shop_id"=>$shop_id,"card_no"=>$value,"face1"=>$face1,"icon1"=>str_replace(ROOT_PATH, "/", $icon1)));	
		}
		db("GymMember")->where("id=$member_id")->setField(["face1_status"=>2,"face1"=>"https://".$_SERVER['HTTP_HOST']."/".str_replace(ROOT_PATH, "/", $icon1)]);
	}
	
	/**调用python 获取人脸模板*/
	function _get_face_code($icon){
		return  str_replace(array("\r\n", "\r", "\n"),"",shell_exec("python /etc/nginx/www/arcface/python/c.py $icon"));
	}
	
}
